
 delete from TBL_TT_CHUOI

 insert TBL_TT_CHUOI
select cust_id, b.cus_name, sum(amount_qd) as amount,c.branch_name_sme, c.BRANCH_ID, c.REGION, fund_program, b.DAO, b.dao_name, c.BRANCH_CODE_SME , b.INDUSTRY_NAME, b.INDUSTRY_NAME_EN, b.CUS_OPEN_DATE
--into TBL_TT_CHUOI
from [SERVER12].[ANHCP].[dbo].[DOANH_SO_BAO_LANH_HTC] a, TBL_CUSTOMER b, TBL_BRANCH c
where a.cust_id = b.CIF and b.BRANCH_ID=c.BRANCH_ID
group by cust_id, b.cus_name,c.branch_name_sme,  c.BRANCH_ID, c.REGION, fund_program, b.dao_name, b.DAO, c.BRANCH_CODE_SME, b.INDUSTRY_NAME, b.INDUSTRY_NAME_EN, b.CUS_OPEN_DATE
 order by cust_id
 
 insert TBL_TT_CHUOI
 select a.cif, customer_name, sum(bal_qd), c.BRANCH_NAME_SME,c.BRANCH_ID, c.region,fund_program, b.DAO, b.dao_name, c.BRANCH_CODE_SME, b.INDUSTRY_NAME, b.INDUSTRY_NAME_EN, b.CUS_OPEN_DATE
from [SERVER12].[ANHCP].[dbo].[CHUOI_CARLSBERG] a, TBL_CUSTOMER b, TBL_BRANCH c
where a.cif= b.cif and b.BRANCH_ID=c.BRANCH_ID
group by a.cif, customer_name, c.BRANCH_NAME_SME,c.BRANCH_ID, c.region,fund_program, b.DAO, b.dao_name, c.BRANCH_CODE_SME, b.INDUSTRY_NAME, b.INDUSTRY_NAME_EN, b.CUS_OPEN_DATE
order by a.cif



 delete from [DBCustomer].dbo.TBL_TT_CHUOI
insert into [DBCustomer].dbo.TBL_TT_CHUOI
select * from TBL_TT_CHUOI

 EXEC('IF EXISTS
      (SELECT *
         FROM [SERVER12].[SMECustomer360].DBO.SYSOBJECTS O
        WHERE O.XTYPE IN (''U'') AND O.NAME = ''TBL_TT_CHUOI'')
        delete from [SERVER12].[SMECustomer360].dbo.TBL_TT_CHUOI')
 
insert [SERVER12].[SMECustomer360].dbo.TBL_TT_CHUOI
select * from TBL_TT_CHUOI